//
//  RankDetailViewController.m
//  Coco_Music
//
//  Created by dlios on 15-6-20.
//  Copyright (c) 2015年 寇广超. All rights reserved.
//

#import "RankDetailViewController.h"
#import "AFN_block.h"
@interface RankDetailViewController ()

@end

@implementation RankDetailViewController
-(void)dealloc
{
    [_myScroView release];
    [_myTableView release];
    [_rank release];
    [_array release];
    [_HUD release];
    [_myView release];
    [_myImageView release];
    [super dealloc];
    
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    self.navigationItem.title = self.rank.title;
    
    
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"正在加载,请稍后";
    [self.HUD show:YES];
    [self getData];
}
-(void)creaeteTable
{
    NSURL *url =[NSURL URLWithString:self.rank.pic_url];
    
    self.myView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height/3)];
    [self.myView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
    [self.view addSubview:self.myView];
    
    
    UIVisualEffectView *visualEffct = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visualEffct.frame=self.myView.frame;
    visualEffct.alpha =1;
    [self.myView addSubview:visualEffct];
    
    
    
    self.myImageView =[[UIImageView alloc]initWithFrame:CGRectMake(self.myView.center.x-70, self.myView.center.y-70 , 140, 140)];
    [self.myImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"cm2_default_artist_banner-1"]];
    
    self.myImageView.layer.masksToBounds =YES;
    self.myImageView.layer.cornerRadius = 10;
    
    [visualEffct addSubview:self.myImageView];
    
    
    
    
    
    self.myTableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height-64-69) style:UITableViewStylePlain];
    self.myTableView.separatorStyle  =  UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:self.myTableView];
    
    
    self.myTableView.tableHeaderView = self.myView;
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myView release];
    
    [_myImageView release];
    [_myLabel release];
    [_myTableView release];
    [_myScroView release];

}
-(void)headerRefesh
{
    self.myTableView.header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.HUD.labelText = @"正在加载,请稍后";
        [self.HUD show:YES];
        [self getData];
        
        [self.myTableView reloadData];
        [self.HUD hide:YES];
        //拿到当前的下拉刷新控件,结束刷新状态
        [self.myTableView.header endRefreshing];
        
        
    }];
   
}



#define kHotsDetailConnect @"http://api.dongting.com/channel/ranklist/%ld/songs?"
#define kHotsDetailConnect1 @"page=1&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=03d04d590225a4fc0d5e5201dfc288d5bd22fe59&idfa=157C61D2-64EC-4453-BCE0-F746F3A64747&utdid=VXlSrtlz43oDAOnVJ3SPXn8M&alf=201200&bundle_id=com.ttpod.music"

-(void)getData
{
    NSString *url1 =[NSString stringWithFormat:kHotsDetailConnect,self.RankDetailID];
    NSString *url =[NSString stringWithFormat:url1,kHotsDetailConnect1];
    [AFN_block NSString:url Blokc:^(id request) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:request];
        NSLog(@"gjkang %@",url);
        
        
        self.array =[NSMutableArray array];
        NSMutableArray *bigArray =[dic objectForKey:@"data"];
        for (NSMutableDictionary *bigDic in bigArray) {
            RankDetail *rank =[[RankDetail alloc]init];
        
            rank.album_name = [bigDic objectForKey:@"albumName"];
            rank.singer_name =[bigDic objectForKey:@"singerName"];
            rank.song_name  = [bigDic objectForKey:@"albumName"];	
            rank.audition_list =[bigDic objectForKey:@"auditionList"];
            rank.favorites =[[bigDic objectForKey:@"favorites"]integerValue];
            rank.duration = [bigDic objectForKey:@"duration"];
            
            [self.array addObject:rank];
           
            
            [self creaeteTable];
            
            
        }
        [self headerRefesh];
        
       
   }];

    
 
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    RankDetailCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell ==nil) {
        cell = [[RankDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];        
    }
   
    cell.rank =[self.array objectAtIndex:indexPath.row];
    cell.numberLabel.text =[NSString stringWithFormat:@"%ld",indexPath.row+1];
    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];

    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayingViewController *playVC = [PlayingViewController shareHandleController];
    playVC.array = self.array;
    playVC.indexPath = indexPath.row;
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
    UINavigationController *naPlayVC = [[UINavigationController alloc]initWithRootViewController:playVC];
    [self presentViewController:naPlayVC animated:YES completion:^{
        
        
    }];
    
    
    
    

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    return 85;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
